package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 获取个人租约基本信息列表
     * @return
     */
    @Override
    public List<AgreementItemVo> getLeaseAgreementByPhone(String phone) {
        return leaseAgreementMapper.listAgreementItemByPhone(phone);
    }

    /**
     * 根据id获取租约详细信息
     * @param id
     * @return
     */
    @Override
    public AgreementDetailVo getAgreementDetailById(Long id) {
        //根据ID查询租约基础信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement == null) return null;
        //查询公寓名称
        String apartmentName = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId()).getName();
        //查询房间号
        String roomNumber = roomInfoMapper.selectById(leaseAgreement.getRoomId()).getRoomNumber();
        //查询公寓图片列表
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        //查询房间图片列表
        List<GraphVo> roomGraphVoList =graphInfoMapper.selectByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        //查询支付方式
        String paymentTypeName = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId()).getName();
        //查询租期月数和单位
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        //查询信息存入实体类
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}




